package com.haha.helloworld.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.haha.helloworld.R;

import androidx.annotation.Nullable;

public class GridViewActivity extends Activity {

    private GridView gv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);
        gv= (GridView)findViewById(R.id.gv);
        gv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity.this,"点击了第"+position+"个item",Toast.LENGTH_SHORT).show();

            }
        });
        //长按事件
        gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(GridViewActivity.this,"长按了第"+position+"个item",Toast.LENGTH_SHORT).show();
                //长按事件，返回true和false是有区别的
                //当你长按完毕之后，松开点击事件会被触发
                //如果希望，长按完毕之后，不触发点击事件，就设置为true
                return true;
            }
        });



    }
}
